package com.hlm.demo.controller.privateinfo;


import com.hlm.demo.common.ResultWrapper;
import com.hlm.demo.entity.AdminInfo;
import com.hlm.demo.entity.ex.AdminWithRole;
import com.hlm.demo.service.adminlogin.IAdminLoginService;
import com.hlm.demo.service.privateinfo.IPrivateInfoService;
import com.hlm.demo.validator.admininfovalidator.AdminLogValidator;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/info")
public class PrivateInfoController {

    @Autowired
    private IAdminLoginService adminLoginService;

    @Autowired
    private IPrivateInfoService privateInfoService;


    @RequestMapping("/pusher")
    @ResponseBody
    public Object infoPusher(HttpServletRequest request) {

        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("adminInfo");

        AdminWithRole adminWithRole = adminLoginService.adminInfoSearcher(username);

        return ResultWrapper.success(adminWithRole);

    }

    @RequestMapping("/infoModify")
    public String infoModify(@Validated(value = {AdminLogValidator.class}) AdminInfo adminInfo, BindingResult result, HttpServletRequest request) {

        if (result.hasErrors()){
            List<FieldError> errors = result.getFieldErrors();
            errors.forEach(System.out::println);
        } else {

        Map<String, Object> newInfoMap = new HashMap<>();
        // 将当前登录用户名放入集合
        newInfoMap.put("adminCode", request.getSession().getAttribute("adminInfo"));

        String adminName = adminInfo.getAdminName();
        String telephone = adminInfo.getTelephone();
        String email = adminInfo.getEmail();
            newInfoMap.put("adminName", adminName);
            newInfoMap.put("telephone", telephone);
            newInfoMap.put("email", email);
        privateInfoService.modifyInfoByName(newInfoMap);
        }
        return "redirect:/user/user_info.html";
    }


    @RequestMapping("/passModify")
    public String passModify(String oldPassword, String newPassword, HttpServletRequest request) {

        System.out.println("old = " + oldPassword.trim());
        System.out.println("new = " + newPassword.trim());

        Map<String, Object> newPassMap = new HashMap<>();
        // 将当前登录用户名放入集合
        newPassMap.put("adminCode", request.getSession().getAttribute("adminInfo"));

        // TODO
        if (!oldPassword.trim().equals("")) {
            newPassMap.put("oldPassword", oldPassword);
        }
        // TODO
        if (!newPassword.trim().equals("")) {
            newPassMap.put("newPassword", newPassword);
        }

        int i = privateInfoService.modifyPassByName(newPassMap);
        System.out.println(i);

        return "redirect:/user/user_modi_pwd.html";
    }


}
